<?php
class ProductModel extends Models
{
    protected $tableName='product';

    public function getProducts($attributes)
    {
        $result=array();
        if($products=$this->findAllByAttributes($attributes)){
            $vendor=$this->model->load('manufacturerModel');
            $prodDescription=$this->model->load('productDescriptionModel');

            foreach($products as $product){
                $manufacturerName='';
                $productName='';
                if($manufacturer=$vendor->findByPk($product['manufacturer_id'])) $manufacturerName=$manufacturer['name'];
                if($description=$prodDescription->findByPk($product['product_id'])) $productName=$description['name'];

                $newProduct=new stdClass();

                $newProduct->Id=$product['product_id'];
                $newProduct->Article=$product['sku'];
                $newProduct->Name=$productName;
                $newProduct->Info=$description;
                $newProduct->Stock=$product['stock_status_id'];
                $newProduct->VendorName=$manufacturerName;
                $newProduct->Cost=$product['price'];
                $newProduct->Duration=$this->getStatusText($product['quantity'], $product['stock_status_id']);
                $newProduct->Timer=$product['timer'];
                $newProduct->SearchId=$description['search_id'];
                $newProduct->Amount=$product['quantity'] ? $product['quantity'] : 0;
                $newProduct->ReplacementId=isset($product['ReplacementId']) ? $product['ReplacementId'] : 0;
                $newProduct->Warehouse=isset($product['Warehouse']) ? $product['Warehouse'] : 0;
                $newProduct->WhQuantity=isset($product['WhQuantity']) ? $product['WhQuantity'] : 0;
                $newProduct->ReplacementGroup=isset($product['ReplacementGroup']) ? $product['ReplacementGroup'] : 0;

                $result[]=$newProduct;
            }

            unset($vendor);
        }

        return $result;
    }

    public function getStatusText($amount, $duration)
    {
        $this->load->language('product/product');
        $text='';

        if((!$amount || $amount=='0' || $amount<0) && (!$duration || $duration=='0')){
            $text=$this->language->get('text_no_available');
        }elseif($duration<0 || !$amount){
            $text=sprintf($this->language->get('text_expectation'), abs($duration));
        }elseif((!$amount || $amount=='0' || $amount<0) && $duration){
            $text=sprintf($this->language->get('text_expectation'), abs($duration));
        }elseif($duration>0 && $amount){
            $text=$this->language->get('text_instock');
        }

        return $text;
    }

    public function getStatusTextById($id, $status=false)
    {
        $product=$this->findModelAttributesByPk($id,array('quantity','stock_status_id'));
        return $this->getStatusText($product['quantity'],$product['stock_status_id'],$status);
    }

    public function getAvailability($amount, $duration, $status=false){
        $this->load->language('product/product');

        if(!$status){
            if($amount && $duration && $amount>0 && $duration>0) $text=$this->language->get('text_instock');
            else $text=$this->language->get('text_no_available');
        }else{
            $text=$this->getStatusText($amount, $duration);
        }

        return $text;
    }

    public function getAvailabilityById($id){
        $product=$this->findModelAttributesByPk($id,array('quantity','stock_status_id'));
        return $this->getAvailability($product['quantity'],$product['stock_status_id']);
    }

    public function getCategoryId($product)
    {
        if($category=$this->model->load('categoryDescriptionModel')->findByAttributes(array('name'=>$product->Vendor))) return $category['category_id'];
        return false;
    }

    public function getManufacturerId($product)
    {
        if(!$product->VendorName) return false;
        if($manufacturer=$this->model->load('manufacturerModel')->findByAttributes(array('name'=>$product->VendorName))) return $manufacturer['manufacturer_id'];
        else $this->model->load('manufacturerModel')->save(array('name'=>$product->VendorName, 'image'=>'', 'sort_order'=>0));
        return $this->db->getLastId();
    }

    public function createProdDescription($product)
    {
        $seo=$this->model->load('seoModel');
        $this->load->model('localisation/language');
        $languages=$this->model_localisation_language->getLanguages();
        $result=array();
        $seoWord=$seo->createSeoWord($product->Name);
        $name=$product->Description;

        foreach($languages as $language){
            if($language['code']==$this->config->get('config_language')) $name=$product->Name;
            $result[$language['language_id']]=array(
                'name'=>$name,
                'seo_h1'=>$seoWord,
                'seo_title'=>$seoWord,
                'meta_keyword'=>$seo->transliterate($product->Name),
                'meta_description'=>'',
                'description'=>$product->Description,
                'tag'=>'',
                'search_id'=>$product->SearchId,
            );
        }

        return $result;
    }

    public function getTableName(){
        return $this->tableName;
    }
}